package com.woniuxy.main.handler;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.main.pojo.Student_coach;
import com.woniuxy.main.pojo.User;
import com.woniuxy.main.service.CoachService;
import com.woniuxy.main.service.StudentService;
import com.woniuxy.main.service.Student_coachService;
import com.woniuxy.main.service.UserService;

import lombok.Data;

@Controller
@RequestMapping("/userAction")
@Scope("prototype")
@Data
public class UserActionHandler {

	@Resource
	private UserService userService;
	@Resource
	private CoachService coachService;
	@Resource
	private StudentService studentService;
	@Resource
	private Student_coachService student_coachService;

	//学员发送私教请求
	@RequestMapping("/sendRequest")
	@ResponseBody
	public String balanceCheck(int uid,int cid,int price){
		System.out.println(uid+" "+cid+" "+price);
		String result = "发送请求失败！";
		double balance = userService.balanceCheck(uid);
		double coachPrice = coachService.searchPriceByCid(cid);
		System.out.println(balance);
		System.out.println(coachPrice);
		//检查前端显示价格与后台价格是否符合
		if(price == coachPrice){
			//检查账户余额是否充足
			if(balance >= coachPrice){
				//创建学员申请表对象
				Student_coach student_coach = new Student_coach();
				//根据用户uid获取学员sid
				int sid = studentService.findSidByUid(uid);
				//获取发送请求时间
				String createDate = new SimpleDateFormat("y-M-d HH:mm:ss").format(new Date());
				//封装数据，创建请求
				student_coach.setSid(sid);
				student_coach.setCid(cid);
				student_coach.setCreateDate(createDate);

				//检查用户是否已经发送过请求
				Student_coach sc = student_coachService.isSendRequest(sid,cid);
				if(sc == null){
					int row = student_coachService.insertRequset(student_coach);
					if(row > 0){
						result = "发送请求成功！";
					}
				}else{
					int row = student_coachService.updateRequest(student_coach);
					if(row > 0){
						result = "发送请求成功！";
					}
				}
			}else{
				result = "账户余额不足，请先充值！";
			}
		}else{
			result = "价格已发生改变！";
		}
		return result;
	}

	//取消约教请求
	@RequestMapping("/cancelRequest")
	@ResponseBody
	public String cancelRequest(int uid,int cid){
		String result = "取消申请失败！";
		//根据用户uid获取学员sid
		int sid = studentService.findSidByUid(uid);
		//创建学员申请表对象
		Student_coach student_coach = new Student_coach();
		student_coach.setSid(sid);
		student_coach.setCid(cid);
		//查询申请状态
		String status = student_coachService.requestStatus(student_coach);
		if(status.equals("等待处理")){
			//取消申请
			int row = student_coachService.deleteRequest(student_coach);
			if(row > 0){
				result = "取消申请成功！";
			} 
		}else{
			result = "申请已被处理，无法取消！";
		}
		return result;
	}

	//根据uid获取用户信息,进入我的钱包
	@RequestMapping("/findUserByUid")
	public ModelAndView findUserByUid(HttpServletRequest request,User user){
		ModelAndView modelAndView = new ModelAndView();
		//获取session
		HttpSession session = request.getSession();
		int uid = (int) session.getAttribute("uid");
		user.setUid(uid);
		//查询用户
		User user1 = userService.findUserByUid(user);
		modelAndView.addObject("user1", user1);
		if(user1.getPayPwd() == null || user1.getPayPwd().equals("")){
			modelAndView.setViewName("/setPwd.html");
		}else{
			modelAndView.setViewName("/myWallet.html");
		}
		return modelAndView;
	}
	
	//设置账户支付密码
	@RequestMapping("/setPwd")
	@ResponseBody
	public String setPwd(HttpServletRequest request,User user){
		String result = "设置支付密码失败！";
		HttpSession session = request.getSession();
		int uid = (int) session.getAttribute("uid");
		user.setUid(uid);
		//将加密前的密码放入session中
		session.setAttribute("payPwd", user.getPayPwd());
		//对密码进行加密(盐值使用uid)
		String newPayPwd=new SimpleHash("MD5",user.getPayPwd(),uid+"",1024).toString();
		user.setPayPwd(newPayPwd);
		//设置用户支付密码
		int row = userService.setPwd(user);
		if(row > 0){
			result = "设置支付密码成功！密码为："+session.getAttribute("payPwd");
		}
		return result;
	}
}
